package com.powerunion.datacollection.quartz.job;

import java.util.Date;

import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import org.quartz.JobExecutionContext;
import com.powerunion.datacollection.common.Format;
import com.powerunion.datacollection.common.SystemException;
import com.powerunion.datacollection.model.CollectionTaskInstance;
import com.powerunion.datacollection.quartz.AppTask;
import com.powerunion.datacollection.service.CollectionTaskInstanceManager;
import com.powerunion.datacollection.service.ProcessInstanceManager;
import com.powerunion.util.log.LogHelper;
/**
 * 任务
 * @author qyw
 *
 */
public class CollectionTask extends AppTask {
	private static Logger logger = LogHelper.getInstance().getLogger(CollectionTask.class);
	public void doTask() {

		JobExecutionContext jobConetext = this.getJobConetext();
		String name = (String)jobConetext.getJobDetail().getJobDataMap().get("name");
		String description = (String)jobConetext.getJobDetail().getJobDataMap().get("description");
		String crtUserCode = (String)jobConetext.getJobDetail().getJobDataMap().get("crtUserCode");
		String formId = (String)jobConetext.getJobDetail().getJobDataMap().get("formId");
		String procId = (String)jobConetext.getJobDetail().getJobDataMap().get("procId");
		String submitTypeStr = (String)jobConetext.getJobDetail().getJobDataMap().get("submitType");
		int submitType = Integer.parseInt(submitTypeStr);
		String periodsTime = (String)jobConetext.getJobDetail().getJobDataMap().get("periodsTime");
		String exactlyTime = (String)jobConetext.getJobDetail().getJobDataMap().get("exactlyTime");
		String periodsPreValue = (String)jobConetext.getJobDetail().getJobDataMap().get("periodsPreValue");
		String periodsLastValue = (String)jobConetext.getJobDetail().getJobDataMap().get("periodsLastValue");
		String taskDefineId = (String)jobConetext.getJobDetail().getJobDataMap().get("id");
		
		CollectionTaskInstance cti = new CollectionTaskInstance();
		cti.setTaskDefineId(taskDefineId);
		cti.setName(name);
		cti.setDescription(description);
		cti.setCreateDate(new Date());
		cti.setCrtUserCode(crtUserCode);
		cti.setFormId(formId);
		cti.setProcId(procId);
		cti.setSubmitType(submitType);
		cti.setPeriodsTime(periodsTime);
		cti.setExactlyTime(exactlyTime);
		cti.setPeriodsPreValue(periodsPreValue);
		cti.setPeriodsLastValue(periodsLastValue);
		
		try{
			
			String createDate = Format.getInstance().dateToString(new Date(), "yyyy-MM-dd HH:mm:ss");
			ProcessInstanceManager pim = new ProcessInstanceManager();
			String itemId = pim.copyProcInfo(cti.getProcId(), cti.getCrtUserCode(),createDate);
			if(!StringUtils.isEmpty(itemId)){
				cti.setProcId(itemId);
				CollectionTaskInstanceManager ctm = new CollectionTaskInstanceManager();
				CollectionTaskInstance collectionTaskInstance = ctm.save(cti);
				if (collectionTaskInstance != null) {
					pim.initSubmissionWorkItems(
							collectionTaskInstance.getProcId(),
							collectionTaskInstance.getFormId());
					//更新任务信息定义表中的任务实例个数字段
//					CollectionTaskManager ctm = new CollectionTaskManager();
//					ctm.updateTaskInstanceNum(collectionTaskInstance
//							.getTaskDefineId());

				} 
			}
			
			
		}catch(Exception e){
			logger.error("", e);
			throw new SystemException("CollectionTask throw error！");
		}
		
	}

}
